Styling Invalid Form Inputs Using :invalid Pseudo-Class in CSS
You can style invalid form inputs directly using the :invalid pseudo-class in CSS. It allows you to provide visual feedback for inputs that fail HTML5 validation constraints without needing JavaScript.
:invalid – Selects form elements whose current value does not satisfy HTML validation requirements (e.g., required fields, pattern mismatches, min/max values).
:valid – Can be used alongside :invalid to style inputs that pass validation.
Styles update dynamically as users interact with the form, giving immediate feedback.
In this example, the email and age fields automatically receive a red border and light red background when their values are invalid, and a green border with light green background when valid, giving clear visual cues to the user.
Combine :invalid with :valid to give real-time validation feedback.
Use additional pseudo-classes like :required or :optional for better user guidance.
Ensure that color cues are accessible; consider adding icons or text messages for users with color vision deficiencies.
Test across browsers, as some older browsers may not fully support HTML5 validation pseudo-classes.
How would you make a required email input turn red when the user submits the form without entering anything?
What CSS rule would you write to highlight a phone input as invalid if it doesn't match a 10-digit pattern?
A form input is visually invalid even though the user entered valid data — what could be wrong with the CSS or HTML structure?
You're told users aren't noticing invalid fields on mobile — how would you improve visibility without JavaScript, and what tradeoffs might you consider?
How would you design a reusable form component library that styles invalid inputs consistently across 20+ forms, while accommodating different design systems and accessibility requirements?
A legacy form uses inline styles and JavaScript for validation — how would you migrate to pure CSS-based validation without breaking existing behavior or accessibility?
You're leading a cross-team initiative to standardize form validation UX across 50+ products — how do you balance CSS-only styling with accessibility, internationalization, and legacy browser support?
How would you architect a validation styling system that scales across micro-frontends, ensuring consistency without shared CSS tooling, and what metrics would you track to measure success?